﻿using System;
using System.Collections;
using System.Collections.Generic;
using Script.WorldEvent;
using TMPro;
using UI;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using EventHandler = Utilities.EventHandler;
using DG.Tweening;
using SciptableObject;

namespace UI
{
    public class HUDController :MonoBehaviour
    {
        [Header("HUD数值设定")]
        [Header("信仰")]
        public float currentFaith;
        public float maxfaith;
        [Header("科技")]
        public float currentTechnology;
        public float maxTechnology;
        [Header("幸福")]
        public float currentHappiness;
        public float maxHappiness;
        [Header("人数")]
        public float currentNumberOfPeople;
        public float maxNumberOfPeople;
        [Header("高度")]
        public float currentHeight;
        [Header("分数")]
        public float scoreValue;
        [Header("球的起始点")]
        public Vector3 rockStartPoint;
        [Header("抽卡消耗")]
        public float cardCost;
        [Header("采样值")]
        //↓↓↓↓↓↓↓↓↓ 4项采样值
        public float faithSampleValue;
        public float happinessSampleValue;
        public float technologySampleValue;
        public float numberOfPeopleSampleValue;
        //↑↑↑↑↑↑↑↑↑↑
        public float samplingInterval;//采样间隔时间
        [Header("对象引用")]
        [Header("数值文本")]
        public TextMeshProUGUI happinessText;
        public TextMeshProUGUI numberOfPeopleText;
        public TextMeshProUGUI techText;
        public TextMeshProUGUI faithText;
        public TextMeshProUGUI heightText;
        [Header("技能控制器")]
        public CardListController PlayerSkillController;

        [Header("球")] public GameObject rock;
        [Header("成功失败弹窗")]
        public SlidePopupWithOffset successPanel;
        public SlidePopupWithOffset failPanel;
        
        public void OnEnable()
        {
            
            EventHandler.FaithValueToUI += UpdateFaith;
            EventHandler.GameDataValueSend += PopQuestTip;
        }

        private void PopQuestTip(List<EventRewordData> arg1, bool arg2)
        {
            if (arg2)
            {
                successPanel.ShowPopup();
                failPanel.HidePopup();
            }
            else
            {
                failPanel.ShowPopup();
                successPanel.HidePopup();
            }
        }

        /// <summary>
        /// 计算更新高度文本
        /// </summary>
        public void UpdateHeightBar()
        {
            this.currentHeight = (int)Vector3.Distance(rock.transform.position, rockStartPoint);
            heightText.text= currentHeight+"基米";
        }

        public void IsGameOver()
        {
            if (currentHappiness <= 0 || currentNumberOfPeople <= 0 || currentTechnology <= 0)
            {
                GameValues gameOverValues = new GameValues();
                gameOverValues.happiness = currentHappiness;
                gameOverValues.numberOfPeople = currentNumberOfPeople;
                gameOverValues.technology = currentTechnology;
                gameOverValues.height = currentHeight;
                EventHandler.CallGameOverValue(gameOverValues);
            }
        }

        public void Start()
        {
            EventHandler.ValueToUI+= UpdateValues;
            GetComponent<Canvas>().worldCamera= Camera.main;
            StartCoroutine(TrytoSampling());
        }


        /// <summary>
        /// 接受并更新HUD控制器的数值（1.当前值 2.最大值）
        /// </summary>
        /// <param name="currentValues"></param>
        /// <param name="maxValues"></param>
        public void UpdateValues(GameValues currentValues)
        {
            Debug.Log("获得：" +currentValues);
            //更新4项基础值的当前值
            this.maxfaith = currentValues.faith;
            this.currentHappiness = currentValues.happiness;
            this.currentNumberOfPeople = currentValues.numberOfPeople;
            this.currentTechnology = currentValues.technology;
            UpdateNumericalDetailsValue();
        }
        /// <summary>
        /// 更新信仰,并限制最大值
        /// </summary>
        /// <param name="faith"></param>
        public void UpdateFaith(float faith)
        {
            if (currentFaith+faith<=maxfaith)//限制最大值
            {
                this.currentFaith += faith;
            }
            else
            {
                this.currentFaith = maxfaith;
            }
        }
        /// <summary>
        /// 尝试消耗资源抽卡
        /// </summary>
        public void TryToExtractCards()
        {
            if (currentFaith >= cardCost&&PlayerSkillController.cardList.Count!=PlayerSkillController.cardSlot.Count)
            {
                faithText.DOColor(Color.red, 0.5f);
                currentFaith -= cardCost;
                PlayerSkillController.ExtractCards();
            }
        }

        /// <summary>
        /// 神秘的采样协程
        /// </summary>
        IEnumerator TrytoSampling()
        {
            while (true)
            {
                yield return new WaitForSeconds(samplingInterval);

                #region 判断，变色
                if (faithSampleValue > currentFaith)
                {
                    faithText.DOColor(Color.red, 0.5f);
                    faithSampleValue = currentFaith;
                }
                else if(faithSampleValue <currentFaith)
                {
                    faithText.DOColor(Color.green, 0.5f);
                    faithSampleValue = currentFaith;
                }
                else
                {
                    faithText.DOColor(Color.white, 0.5f);
                     faithSampleValue = currentFaith;    
                }
                
                if (happinessSampleValue > currentHappiness)
                {
                    happinessText.DOColor(Color.red, 0.5f);
                    happinessSampleValue = currentHappiness;
                }
                else if(faithSampleValue <currentFaith)
                {
                    happinessText.DOColor(Color.green, 0.5f);
                    happinessSampleValue = currentHappiness;
                }
                else
                {
                    happinessText.DOColor(Color.white, 0.5f);
                     happinessSampleValue = currentHappiness;   
                }

                if (numberOfPeopleSampleValue > currentNumberOfPeople)
                {
                    numberOfPeopleText.DOColor(Color.red, 0.5f);
                    numberOfPeopleSampleValue = currentNumberOfPeople;
                }
                else if (faithSampleValue < currentFaith)
                {
                    numberOfPeopleText.DOColor(Color.green, 0.5f);
                    numberOfPeopleSampleValue = currentNumberOfPeople;
                }
                else
                {
                    numberOfPeopleText.DOColor(Color.white, 0.5f);
                    numberOfPeopleSampleValue = currentNumberOfPeople;
                }

                if (technologySampleValue > currentTechnology)
                {
                    techText.DOColor(Color.red, 0.5f);
                    technologySampleValue = currentTechnology;
                }
                else if (faithSampleValue < currentFaith)
                {
                    techText.DOColor(Color.green, 0.5f);
                    technologySampleValue = currentTechnology;
                }
                else
                {
                    techText.DOColor(Color.white, 0.5f);
                    technologySampleValue = currentTechnology;
                }
                #endregion
            }
        }
        




        public void Update()
        {
            if (rock == null)
            {
                rock = GameObject.FindWithTag("Rock");
            }
            else if(rockStartPoint== Vector3.zero)
            {
                rockStartPoint = rock.transform.position;
            }
            else
            {
                UpdateHeightBar();
            }
            UpdateNumericalDetailsValue();
            
            if (Input.GetKeyDown(KeyCode.D))
            {
                TryToExtractCards();

            }

            IsGameOver();
        }

        public void UpdateNumericalDetailsValue() 
        {   //更新数值文本
            faithText.text= currentFaith.ToString();
            happinessText.text = currentHappiness.ToString();
            numberOfPeopleText.text = currentNumberOfPeople.ToString();
            techText.text = currentTechnology.ToString();
        }
    }
}